package config

import (
	"common/helper"
	"database/sql"
	"github.com/go-redis/redis/v8"
	"github.com/jmoiron/sqlx"
)

var CfgSql *sqlx.DB
var CfgRedis *redis.Client

type FuncRowSetCache func(row *sql.Rows)

type StLoadCfgCommon struct {
	selectSql       string
	funcRowSetCache FuncRowSetCache //加载单行数据
	funcClear       func()
}

var g_mapCfgCommon map[string] /*table name*/ StLoadCfgCommon

func LoadAllCfg() {
	if nil == CfgSql {
		helper.InfoLog("LoadAllCfg CfgSql nil")
		return
	}

	//ClearAllTable()

	for _, CfgFuncInfo := range g_mapCfgCommon {
		LoadTable(&CfgFuncInfo)
	}
}

func LoadTable(tableFunc *StLoadCfgCommon) {

	rows, err := CfgSql.Query(tableFunc.selectSql)
	if err != nil {
		helper.SqlSelectErrLog("%s,%s", err.Error(), tableFunc.selectSql)
		return
	}

	tableFunc.funcClear()

	i := 0
	for rows.Next() {
		tableFunc.funcRowSetCache(rows)
		i++
	}

	//	helper.InfoLog("cfg load,%s,row：%d", tableFunc.selectSql, i)
	if i == 0 {
		helper.SqlSelectErrLog("LoadTable,row is 0 %s", tableFunc.selectSql)
	}
}

func ClearAllTable() {
	for _, CfgFuncInfo := range g_mapCfgCommon {
		CfgFuncInfo.funcClear()
	}
}

func ReloadTableByName(tableName string) {
	tableFunc, ext := g_mapCfgCommon[tableName]
	if !ext {
		helper.InfoLog("g_mapCfgCommon not table %s", tableName)
		return
	}

	tableFunc.funcClear()
	LoadTable(&tableFunc)
}

func InitCfgMgr() {

	g_mapCfgCommon = make(map[string]StLoadCfgCommon)

	g_mapCfgCommon["tbl_cfg_week_deposit_return_rate"] = StLoadCfgCommon{
		selectSql:       "select deposit,return_rate from tbl_cfg_week_deposit_return_rate order by deposit asc",
		funcRowSetCache: DepositReturnCfgSetRow,
		funcClear:       DepositReturnCfgRemove,
	}

	g_mapCfgCommon["tbl_game_config"] = StLoadCfgCommon{
		selectSql:       "SELECT cfg_type,cfg_value,description FROM tbl_game_config",
		funcRowSetCache: GameCfgSetRow,
		funcClear:       GameCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_week_lost_return_rate"] = StLoadCfgCommon{
		selectSql:       "select running,return_rate from tbl_cfg_week_lost_return_rate order by running asc",
		funcRowSetCache: LostReturnSetRow,
		funcClear:       LostReturnRemove,
	}
	g_mapCfgCommon["tbl_cfg_week_proxy_commi_return_rate"] = StLoadCfgCommon{
		selectSql:       "select deposit,return_rate from tbl_cfg_week_proxy_commi_return_rate order by deposit asc",
		funcRowSetCache: ProxyCommiReturnSetRow,
		funcClear:       ProxyCommiReturnRemove,
	}

	g_mapCfgCommon["tbl_cfg_week_running_return_rate"] = StLoadCfgCommon{
		selectSql:       "select running,return_rate from tbl_cfg_week_running_return_rate order by running asc",
		funcRowSetCache: RunningReturnCfgSetRow,
		funcClear:       RunningReturnCfgRemove,
	}

	g_mapCfgCommon["lottery_config"] = StLoadCfgCommon{
		selectSql: "select open_hour,is_enabled,mode,deduction_rate,base_award,min_recharge_amount," +
			"greedy_rate,discount_rate,h5_refresh_interval_s,rule_text from lottery_config",
		funcRowSetCache: LotteryCfgSetRow,
		funcClear:       LotteryCfgCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_accu_deposit_bonus"] = StLoadCfgCommon{
		selectSql:       "select level,accu_deposit_amount,bonus from tbl_cfg_accu_deposit_bonus order by accu_deposit_amount asc",
		funcRowSetCache: AccuDepositBonusCfgSetRow,
		funcClear:       AccuDepositBonusCfgRemove,
	}

	g_mapCfgCommon["tbl_promo_deposit"] = StLoadCfgCommon{
		selectSql:       "select name,bonus,flow,max_amount,min_amount,ty from tbl_promo_deposit order by min_amount desc ",
		funcRowSetCache: PromoDepositCfgSetRow,
		funcClear:       PromoDepositCfgRemove,
	}

	//add cgy

	g_mapCfgCommon["tbl_game_lists"] = StLoadCfgCommon{
		selectSql:       "select game_id,platform_id,game_type,maintained,to_fake,online from tbl_game_lists",
		funcRowSetCache: GameListCfgSetRow,
		funcClear:       GameListCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_fakegame_route_info"] = StLoadCfgCommon{
		selectSql:       "select platform_id,status,fee,priority,name,game,test from tbl_cfg_fake_route_info order by priority asc ",
		funcRowSetCache: FakeGameRouteCfgSetRow,
		funcClear:       FakeGameRouteRemoveAllCfg,
	}

	g_mapCfgCommon["tbl_cfg_fakegame"] = StLoadCfgCommon{
		selectSql:       "select game,game_id,fake_game_id,status,game_name,platform_id from tbl_cfg_fake",
		funcRowSetCache: FakeGameCfgSetRow,
		funcClear:       FakeGameCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_vip"] = StLoadCfgCommon{
		selectSql:       "select level,needRunning,upBonus,weekBonus,monthBonus from tbl_cfg_vip order by level asc ",
		funcRowSetCache: CfgVipSetRow,
		funcClear:       removeAllVipCfg,
	}

	g_mapCfgCommon["tbl_cfg_active_switch"] = StLoadCfgCommon{
		selectSql:       "select active_type,active_name,status,popup_switch from tbl_cfg_active_switch",
		funcRowSetCache: ActiveSwitchCfgSetRow,
		funcClear:       ActiveSwitchCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_proxy_invite_bonus_by_deposit"] = StLoadCfgCommon{
		selectSql:       "select deposit, bonus from tbl_cfg_proxy_invite_bonus_by_deposit order by deposit asc ",
		funcRowSetCache: ProxyInviteBonusByDepositAmountCfgSetRow,
		funcClear:       ProxyInviteBonusByDepositAmountCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_proxy_invite_bonus"] = StLoadCfgCommon{
		selectSql:       "select valid_Counts, bonus,id from tbl_cfg_proxy_invite_bonus order by id asc ",
		funcRowSetCache: ProxyInviteBonusCfgSetRow,
		funcClear:       ProxyInviteBonusCfgRemove,
	}

	g_mapCfgCommon["tbl_flow_config"] = StLoadCfgCommon{
		selectSql:       "select ty,flow_multiple from tbl_flow_config order by ty asc",
		funcRowSetCache: FlowCfgSetRow,
		funcClear:       FlowCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_channel_waged"] = StLoadCfgCommon{
		selectSql:       "select channel_id,ty,flow_multiple from tbl_cfg_channel_waged order by channel_id asc",
		funcRowSetCache: ChannelWagedCfgSetRow,
		funcClear:       ChannelWagedCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_thirdgame"] = StLoadCfgCommon{
		selectSql:       "select platformId,taxSwitch,taxRate from tbl_cfg_thirdgame order by platformId asc",
		funcRowSetCache: ThirdGameCfgSetRow,
		funcClear:       ThirdGameCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_disablebind"] = StLoadCfgCommon{
		selectSql:       "select InviteCounts,DisableBindRate,max_disableBind,operator_id from tbl_cfg_disablebind order by InviteCounts desc",
		funcRowSetCache: DisableBindCfgSetRow,
		funcClear:       DisableBindCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_valid_waged_rate"] = StLoadCfgCommon{
		selectSql:       "select bet_amount,valid_waged_rate from tbl_cfg_valid_waged_rate order by bet_amount desc",
		funcRowSetCache: AddValidWagedRateSetRow,
		funcClear:       AddValidWagedRateRemove,
	}

	g_mapCfgCommon["tbl_cfg_turntable"] = StLoadCfgCommon{
		selectSql:       "select id,type,lucky_point,rate from tbl_cfg_turntable order by id asc",
		funcRowSetCache: AddTurnTableSetRow,
		funcClear:       TurnTableSetRowRemove,
	}

	g_mapCfgCommon["tbl_cfg_entryfakepg"] = StLoadCfgCommon{
		selectSql:       "select TotalWin,EntryFakePGRate from tbl_cfg_entryfakepg order by TotalWin desc",
		funcRowSetCache: EntryFakePGCfgSetRow,
		funcClear:       EntryFakePGCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_daily_running_return_rate"] = StLoadCfgCommon{
		selectSql:       "select running,return_rate from tbl_cfg_daily_running_return_rate order by running asc",
		funcRowSetCache: DailyRunningReturnCfgSetRow,
		funcClear:       DailyRunningReturnCfgRemove,
	}

	//add gjf
	g_mapCfgCommon["tbl_cfg_announcement"] = StLoadCfgCommon{
		selectSql:       "select  id,annou_url as images,annou_share_url as redirect_url,sort,length,width,jump_type,content,func_type,annou_title from tbl_cfg_announcement where nullity=0  order by sort asc",
		funcRowSetCache: AnnouncementSetRow,
		funcClear:       AnnouncementRemove,
	}

	g_mapCfgCommon["tbl_business_info"] = StLoadCfgCommon{
		selectSql:       "select id, operator_id from tbl_business_info",
		funcRowSetCache: BussinessInfoSetRow,
		funcClear:       BussinessInfoRemove,
	}

	g_mapCfgCommon["tbl_operator_info_fake"] = StLoadCfgCommon{
		selectSql: "select operator_id,game,into_fake_rate,init_rtp,adjust_rtp,adjust_money_mul" +
			" from tbl_operator_info_fake",
		funcRowSetCache: ChannelInfoSetRow,
		funcClear:       ChannelInfoRemove,
	}

	g_mapCfgCommon["tbl_cfg_add_wage"] = StLoadCfgCommon{
		selectSql:       "select win_amount,wage from tbl_cfg_add_wage order by win_amount desc",
		funcRowSetCache: AddWageSetRow,
		funcClear:       AddWageRemove,
	}

	g_mapCfgCommon["tbl_cfg_tax"] = StLoadCfgCommon{
		selectSql:       "select id,profit_amount,tax_rate from tbl_cfg_tax order by profit_amount asc",
		funcRowSetCache: TaxCfgSetRow,
		funcClear:       TaxCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_customer_service"] = StLoadCfgCommon{
		selectSql:       "select id,avatar,customer_service_name,title,customer_service_type,open_method,customer_service_link,state from tbl_cfg_customer_service",
		funcRowSetCache: CustomerServiceCfgSetRow,
		funcClear:       CustomerServiceCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_broadcast_config"] = StLoadCfgCommon{
		selectSql:       "select id,type,phone,amount,content,confirm_at from tbl_cfg_broadcast_config order by confirm_at desc",
		funcRowSetCache: BroadcastCfgSetRow,
		funcClear:       BroadcastCfgRemove,
	}

	g_mapCfgCommon["tbl_cfg_suspension_images"] = StLoadCfgCommon{
		selectSql:       "select  id,images,jump_type,sort,status,display_method,url,pc_images,length,width,title from tbl_cfg_suspension_images  order by sort asc",
		funcRowSetCache: SuspensionImagesSetRow,
		funcClear:       SuspensionImagesRemove,
	}

	g_mapCfgCommon["tbl_cfg_email_suffix_blacklist"] = StLoadCfgCommon{
		selectSql:       "select email_suffix from tbl_cfg_email_suffix_blacklist where state=1",
		funcRowSetCache: MailSuffixSetRow,
		funcClear:       MailSuffixRemove,
	}

	//add yb

}
